
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>8.变量的命名 - fubaisen</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="变量的命名目标
标识符和关键字
变量的命名规则

0.1 标识符和关键字1.1 标识符
标示符就是程序员定义的 变量名、函数名
名字 需要有 见名知义 的效果，见下图：



标示符可以由 字母、下,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="fubaisen" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    
    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="8.变量的命名 - fubaisen"/>
    <meta name="twitter:description" content="变量的命名目标
标识符和关键字
变量的命名规则

0.1 标识符和关键字1.1 标识符
标示符就是程序员定义的 变量名、函数名
名字 需要有 见名知义 的效果，见下图：



标示符可以由 字母、下,"/>
    
    
    
    
    <meta property="og:site_name" content="fubaisen"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="8.变量的命名 - fubaisen"/>
    <meta property="og:description" content="变量的命名目标
标识符和关键字
变量的命名规则

0.1 标识符和关键字1.1 标识符
标示符就是程序员定义的 变量名、函数名
名字 需要有 见名知义 的效果，见下图：



标示符可以由 字母、下,"/>
    
<link rel="stylesheet" href="/css/diaspora.css">

    <script>window.searchDbPath = "/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">fubaisen</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://fubaisen.github.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">8.变量的命名</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">8.变量的命名</h1>
        <div class="stuff">
            <span>十二月 23, 2021</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/tags/python%E5%AD%A6%E4%B9%A0/" rel="tag">python学习</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="变量的命名"><a href="#变量的命名" class="headerlink" title="变量的命名"></a>变量的命名</h1><h2 id="目标"><a href="#目标" class="headerlink" title="目标"></a>目标</h2><ul>
<li>标识符和关键字</li>
<li>变量的命名规则</li>
</ul>
<h2 id="0-1-标识符和关键字"><a href="#0-1-标识符和关键字" class="headerlink" title="0.1 标识符和关键字"></a>0.1 标识符和关键字</h2><h3 id="1-1-标识符"><a href="#1-1-标识符" class="headerlink" title="1.1 标识符"></a>1.1 标识符</h3><blockquote>
<p>标示符就是程序员定义的 <strong>变量名</strong>、<strong>函数名</strong></p>
<p><strong>名字</strong> 需要有 <strong>见名知义</strong> 的效果，见下图：</p>
</blockquote>
<p><img src="https://s2.loli.net/2023/03/15/YRnxMCfzvKLWwu2.jpg" alt="001_中国山东找蓝翔"></p>
<ul>
<li>标示符可以由 <strong>字母</strong>、<strong>下划线</strong> 和 <strong>数字</strong> 组成</li>
<li><strong>不能以数字开头</strong></li>
<li><strong>不能与关键字重名</strong></li>
</ul>
<p>思考：下面的标示符哪些是正确的，哪些不正确为什么？</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">fromNo12</span><br><span class="line">from#12      错误有#</span><br><span class="line">my_Boolean</span><br><span class="line">my-Boolean   错误有-</span><br><span class="line">Obj2</span><br><span class="line">2ndObj       错误是数字开头</span><br><span class="line">myInt</span><br><span class="line">My_tExt</span><br><span class="line">_test</span><br><span class="line">test!32      错误有！</span><br><span class="line">haha(da)tt   错误有（）</span><br><span class="line">jack_rose</span><br><span class="line">jack&amp;rose    错误有&amp;</span><br><span class="line">GUI</span><br><span class="line">G.U.I        错误有.</span><br></pre></td></tr></table></figure>

<h3 id="1-2-关键字"><a href="#1-2-关键字" class="headerlink" title="1.2 关键字"></a>1.2 关键字</h3><ul>
<li><strong>关键字</strong> 就是在 <code>Python</code> 内部已经使用的标识符</li>
<li><strong>关键字</strong> 具有特殊的功能和含义</li>
<li>开发者 <strong>不允许定义和关键字相同的名字的标示符</strong></li>
</ul>
<p>通过以下命令可以查看 <code>ipython</code> 中的关键字</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">In [<span class="number">1</span>]: <span class="keyword">import</span> keyword</span><br><span class="line">In [<span class="number">2</span>]: <span class="built_in">print</span>(keyword.kwlist)</span><br></pre></td></tr></table></figure>

<blockquote>
<p>提示：<strong>关键字的学习及使用</strong>，会在后面的课程中不断介绍</p>
<ul>
<li><p><code>import</code> <strong>关键字</strong> 可以导入一个 <strong>“工具包”</strong></p>
</li>
<li><p>在 <code>Python</code> 中不同的工具包，提供有不同的工具</p>
</li>
</ul>
</blockquote>
<h2 id="02-变量的命名规则"><a href="#02-变量的命名规则" class="headerlink" title="02. 变量的命名规则"></a>02. 变量的命名规则</h2><blockquote>
<p><strong>命名规则</strong> 可以被视为一种 <strong>惯例</strong>，并无绝对与强制<br>目的是为了 <strong>增加代码的识别和可读性</strong></p>
</blockquote>
<p><strong>注意</strong> <code>Python</code> 中的 <strong>标识符</strong> 是 <strong>区分大小写的</strong></p>
<p><img src="https://s2.loli.net/2023/03/15/gLe2TFfJCVakotq.png" alt="002_标识符区分大小写"></p>
<ol>
<li>在定义变量时，为了保证代码格式，<code>=</code> 的左右应该各保留一个空格</li>
<li>在 <code>Python</code> 中，如果 <strong>变量名</strong> 需要由 <strong>二个</strong> 或 <strong>多个单词</strong> 组成时，可以按照以下方式命名<ol>
<li>每个单词都使用小写字母</li>
<li>单词与单词之间使用 <strong><code>_</code>下划线</strong> 连接</li>
</ol>
<ul>
<li>例如：<code>first_name</code>、<code>last_name</code>、<code>qq_number</code>、<code>qq_password</code></li>
</ul>
</li>
</ol>
<h3 id="驼峰命名法（其他语言常用到）"><a href="#驼峰命名法（其他语言常用到）" class="headerlink" title="驼峰命名法（其他语言常用到）"></a>驼峰命名法（其他语言常用到）</h3><ul>
<li>当 <strong>变量名</strong> 是由二个或多个单词组成时，还可以利用驼峰命名法来命名</li>
<li><strong>小驼峰式命名法</strong><ul>
<li>第一个单词以小写字母开始，后续单词的首字母大写</li>
<li>例如：<code>firstName</code>、<code>lastName</code></li>
</ul>
</li>
<li><strong>大驼峰式命名法</strong><ul>
<li>每一个单词的首字母都采用大写字母</li>
<li>例如：<code>FirstName</code>、<code>LastName</code>、<code>CamelCase</code></li>
</ul>
</li>
</ul>
<p><img src="https://s2.loli.net/2023/03/15/aKPXMNAgRytEfbd.jpg" alt="003_驼峰命名法"></p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
</div>


    </div>
</div>
</body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/typed.js"></script>
<script src="/js/diaspora.js"></script>


<link rel="stylesheet" href="/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/photoswipe/default-skin/default-skin.css">


<script src="/photoswipe/photoswipe.min.js"></script>
<script src="/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
